#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <string.h>
#include "smsh.h"

/**
  * first really useful version after prompting shell
  * this one parses the command line into strings 
  * uses fork, exec, wait, and ignores signals
  */

#define DFL_PROMPT ">"

int main() {
	char *cmdline, *prompt, **arglist;
	int result;
	void setup();

	prompt = DFL_PROMPT;
	setup();

	while ((cmdline = next_cmd(prompt, stdin)) != NULL) {
		if (strcmp(cmdline, "exit") == 0) {
		 break;
		}
		//printf("cmdline = %s\n", cmdline);
		if ((arglist = splitline(cmdline)) != NULL) {
			result = process(arglist);
			freelist(arglist);
		}
	}
	return 0;
}


/**
  * purpose: initialize shell, ignore SIGINT and SIGQUIT
  * returns: nothing
  * exception handle: calls fatal() if trouble
  */
void setup() {
	extern char **environ;
	VLenviron2table(environ);
	signal(SIGINT, SIG_IGN);
	signal(SIGQUIT, SIG_IGN);
}


void fatal(char *s1, char *s2, int n) {
	fprintf(stderr, "Error, %s, %s\n", s1, s2);
	exit(n);
}
